<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局  头部布局-->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体内容的布局 -->
        <!-- :data指定要渲染的数据 -->
        <!-- :props指定数据中的对象的某个属性在树形组件显示及子级节点的属性名 -->
        <!-- @node-click:点击事件,点击某一个节点,获取节点对象数据 -->
        <el-tree :data="list" @node-click="handleNodeClick">
          <!-- 使用插件在el-tree内容自定义内容 -->
          <!-- 在template上使用默认插槽  #default="{data}" -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 准备弹窗放入新增部分的子组件 -->
      <el-dialog
        :title="isEdit? '编辑部门' :'新增部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="true"
        @close="hClose"
      >
        <!-- 新增子部门或编辑部门的子组件 -->
        <add-or-edit
          v-if="showDialog"
          :id="curId"
          ref="myDept"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/department'
import { tranListToTreeList } from '@/utils/tranListToTreeList'
import addOrEdit from '@/views/departments/deptDialog.vue'

export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false, // 控制弹窗的的显示与隐藏
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.$refs.myDept.resetForm()
    },
    hSuccess() {
      // 关闭弹窗  刷新页面
      this.showDialog = false
      this.loadDepartments()
    },
    async  loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res)
        // this.list = res.data.depts
        this.list = tranListToTreeList(res.data.depts)
        this.originList = res.data.depts.map(item => {
          const { code, id, pid, name } = item
          return { code, id, pid, name }
        })
        this.list.shift()
      } catch (error) {
        console.log(error)
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // 解决数据不更新的方式二: 在父组件中调用子组件中的方法
      // this.$nextTick(() => {
      //   this.$refs.myDepts.loadDepartmentDetail()
      // })
    },
    hDel(id) {
      this.$confirm('你确定要删除部门吗?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartment(id)
        console.log(res)
        if (res.success) {
          this.loadDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleNodeClick() {
      console.log('1')
    }
  }
}
</script>
